//
//  TestElementsViewController.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2023/7/10.
//  Copyright © 2023 moon. All rights reserved.
//

#import "TestElementsViewController.h"
#import "Menu.h"
#import "OpertaionBlock.h"
#import "MKUIElementListViewCellSwitch+Test.h"
#import "MKUIElementListViewCellSelection+Test.h"
#import "MKUIElementListViewCellButton+Test.h"
#import "TestMKUIListViewSectionHeadFootView.h"
#import "TestElementHeaders.h"
#import "TestFunButton.h"

@interface __TestListViewBackgroundView : UIView
@property(nonatomic,strong) UIView *innerBG;
@property(nonatomic,assign) UIEdgeInsets contentInsets;
@end
@implementation __TestListViewBackgroundView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.innerBG = [[UIView alloc] init];
        self.innerBG.layer.borderColor = UIColor.redColor.CGColor;
        self.innerBG.layer.borderWidth = 4;
//        self.contentInsets = UIEdgeInsetsMake(-0, -22, 0, -22);
        [self addSubview:self.innerBG];
        self.backgroundColor = UIColor.lightGrayColor;
        self.layer.borderColor = UIColor.blueColor.CGColor;
        self.layer.borderWidth = 2;
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    UIEdgeInsets safeAreaInsets = UIEdgeInsetsZero;
    if (@available(iOS 11.0, *)) {
        safeAreaInsets = self.safeAreaInsets;
    }
    CGRect f1 = UIEdgeInsetsInsetRect(self.bounds, safeAreaInsets);
    f1 = UIEdgeInsetsInsetRect(f1, self.contentInsets);
    f1 = UIEdgeInsetsInsetRect(f1, self.mk_listView.contentInset);
    self.innerBG.frame = f1;
    {
        UIEdgeInsets safeAreaInsets = UIEdgeInsetsZero;
        UIEdgeInsets safeAreaInsets_listView = UIEdgeInsetsZero;
        UIEdgeInsets safeAreaInsets_innerBG = UIEdgeInsetsZero;
        if (@available(iOS 11.0, *)) {
            safeAreaInsets = self.safeAreaInsets;
            safeAreaInsets_listView = self.mk_listView.safeAreaInsets;
            safeAreaInsets_innerBG = self.innerBG.safeAreaInsets;
        }
        NSLog(@"__TestListViewBackgroundView,safeAreaInsets:%@,safeAreaInsets_listView:%@,safeAreaInsets_innerBG:%@",NSStringFromUIEdgeInsets(safeAreaInsets),NSStringFromUIEdgeInsets(safeAreaInsets_listView),NSStringFromUIEdgeInsets(safeAreaInsets_innerBG));
    }
}
- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
}
@end

@interface __TestSizeLabelCell : MKUIListSelectionCellViewBase
@property(nonatomic,strong) UILabel *label;
@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
@end
@implementation __TestSizeLabelCell
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.label = [[UILabel alloc] init];
        self.label.numberOfLines = 0;
        self.label.text = @"";
        self.label.font = [UIFont systemFontOfSize:15];
        self.label.textColor = [UIColor mk_colorWithLight:UIColor.blackColor];
        [self addTextWithLength:20];
        [self addSubview:self.label];
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.label] constraintParam:(MKUIFlowLayoutConstraintParam_H_C_L) contentInsets:UIEdgeInsetsZero interitemSpacing:5];
    }
    return self;
}
- (void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    NSLog(@"__TestSizeLabelCell:setFrame:%@",NSStringFromCGRect(frame));
}
- (void)layoutSubviews{
    [super layoutSubviews];
    UIEdgeInsets safeAreaInsets = UIEdgeInsetsZero;
    if (@available(iOS 11.0, *)) {
        safeAreaInsets = self.safeAreaInsets;
    }
    self.flowlayout.contentInsets = safeAreaInsets;
    self.flowlayout.bounds = self.bounds;
    [self.flowlayout layoutItemsWithResizeItems:YES];
    NSLog(@"__TestSizeLabelCell:layoutSubviews:%@,safeAreaInsets:%@",NSStringFromCGRect(self.bounds),NSStringFromUIEdgeInsets(safeAreaInsets));
}
- (CGSize)sizeThatFits:(CGSize)size{
    UIEdgeInsets safeAreaInsets = UIEdgeInsetsZero;
    if (@available(iOS 11.0, *)) {
        safeAreaInsets = self.safeAreaInsets;
    }
    self.flowlayout.contentInsets = safeAreaInsets;
    CGSize s = [self.flowlayout sizeThatFits:size resizeItems:YES];
    NSLog(@"__TestSizeLabelCell:sizeThatFits:%@,result:%@",NSStringFromCGSize(size),NSStringFromCGSize(s));
    return s;
}
- (void)addTextWithLength:(NSInteger)len{
    NSMutableString *s = [[NSMutableString alloc] initWithString:self.label.text?:@""];
    for(int i=0;i<len;i++){
        [s appendString:@"1"];
    }
    self.label.text = s;
}
- (void)didClick{
    [super didClick];
    [self addTextWithLength:10];
    [self mk_reloadInListView];
}
@end


@interface __TestEmptyListCell : MKUIListSelectionCellViewBase
@end
@implementation __TestEmptyListCell
- (CGSize)sizeThatFits:(CGSize)size{
    return CGSizeZero;
}
- (void)listView:(MKUIListView *)listView willDisplayAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"展示空白单元格:%@,%@",indexPath,self);
}
- (void)listView:(MKUIListView *)listView didEndDisplayingAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"隐藏空白单元格:%@,%@",indexPath,self);
}
@end
@interface __TestEmptyListHeadFoot : UIView<MKUIListSectionViewProtocol>
@end
@implementation __TestEmptyListHeadFoot
- (CGSize)sizeThatFits:(CGSize)size{
    return CGSizeZero;
}
- (void)listView:(MKUIListView *)listView willDisplayAtSection:(NSInteger)section kind:(MKUIListViewSectionViewKind)kind{
    NSLog(@"展示空白分组:%@-%@,%@",@(section),@(kind),self);
}
- (void)listView:(MKUIListView *)listView didEndDisplayingAtSection:(NSInteger)section kind:(MKUIListViewSectionViewKind)kind{
    NSLog(@"隐藏空白分组:%@-%@,%@",@(section),@(kind),self);
}
@end

@implementation TestButtonOption
+ (TestButtonOption *)optionWithText:(NSString *)text action:(TestButtonOptionBlock)action{
    TestButtonOption *option = [[TestButtonOption alloc] init];
    option.text = text;
    option.action = action;
    return option;
}
@end

@interface __TestButtonOptionButton : MKUIFlowLayoutButton
@property(nonatomic,strong) TestButtonOption *buttonOption;
@end
@implementation __TestButtonOptionButton
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        [self mk_addClickActionBlock:^(__TestButtonOptionButton *self) {
            if(self.buttonOption.action){
                self.buttonOption.action(self.buttonOption);
            }
        } context:self];
        [self setTitleColor:UIColor.systemBlueColor forState:(UIControlStateNormal)];
        self.titleLabel.font = [UIFont systemFontOfSize:16];
        self.contentInsets = UIEdgeInsetsMake(0, 12, 0, 12);
        self.minHitSize = CGSizeMake(44, 44);
    }
    return self;
}
- (void)setButtonOption:(TestButtonOption *)buttonOption{
    if(_buttonOption==buttonOption)return;
    _buttonOption = buttonOption;
    [self refreshButtonOption];
}
- (void)refreshButtonOption{
    [self setTitle:self.buttonOption.text forState:(UIControlStateNormal)];
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize s = [super sizeThatFits:CGSizeMake(9999, size.height)];
    s.height = MAX(30,s.height);
    return s;
}
@end

@interface TestOptionButtonsListViewCell()
@property(nonatomic,strong) UILabel *infoLabel;
@property(nonatomic,strong) MKUIListView *listView;
@property(nonatomic,strong) MKUIFlowLayoutConstraint *flowlayout;
@end

@implementation TestOptionButtonsListViewCell
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        //
        self.infoLabel = [[UILabel alloc] init];
        self.infoLabel.numberOfLines = 0;
        [self.contentView addSubview:self.infoLabel];
        //
        self.listView = [[MKUIListView alloc] init];
        self.listView.style = MKUIListViewStylePlain;
        self.listView.scrollDirection = MKUIListViewScrollDirectionHorizontal;
        self.listView.separatorStyle = MKUIListViewCellSeparatorStyleSingleLine;
        self.listView.separatorColor = UIColor.systemBlueColor;
        self.listView.separatorSize = 1.0/UIScreen.mainScreen.scale;
        self.listView.backgroundColor = UIColor.clearColor;
        self.listView.groupBackgroundColor = UIColor.clearColor;
        [self.contentView addSubview:self.listView];
        @MK_WEAKIFY(self);
        MKUILayoutConstraintItemWrapper *w2 = [MKUILayoutConstraintItemWrapper wrapItem:self.listView sizeThatFitsBlock:^CGSize(MKUILayoutConstraintItemWrapper * _Nonnull wrapper, CGSize size, BOOL resizeItems) {
            @MK_NORMALIZE(self);
            CGSize s = [self.listView sizeThatFits:size unlimit:YES];
            return s;
        }];
        //
        self.flowlayout = [[MKUIFlowLayoutConstraint alloc] initWithItems:@[self.infoLabel,w2] constraintParam:(MKUIFlowLayoutConstraintParam_V_C_C) contentInsets:MKUIEdgeInsetsMakeSameEdge(10) interitemSpacing:10];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.contentView.bounds;
    CGRect f1 = bounds;
    self.flowlayout.bounds = f1;
    [self.flowlayout layoutItemsWithResizeItems:YES];
//    f1.size.width = [self.listView sizeThatFits:bounds.size unlimit:YES].width;
//    f1.size.width = MIN(bounds.size.width,f1.size.width);
//    MKCGRectAlignMidXToRect(&f1, bounds);
//    self.listView.frame = f1;
    [self.listView layoutIfNeeded];
    [self.listView mk_autoBounces];
}
- (void)refreshButtons{
    [self.listView deleteAllSections];
    [self.listView addSection];
    for(TestButtonOption *option in self.buttonOptions){
        __TestButtonOptionButton *button = [[__TestButtonOptionButton alloc] init];
        button.buttonOption = option;
        [self.listView addItem:button];
    }
    [self.listView reloadData];
    [self _reloadData];
    [self setNeedsLayout];
}
- (void)safeAreaInsetsDidChange{
    [super safeAreaInsetsDidChange];
    [self mk_reloadInListView];
    [self _reloadData];
}
- (void)_reloadData{
    UIEdgeInsets safeAreaInsets = UIEdgeInsetsZero;
    if (@available(iOS 11.0, *)) {
        safeAreaInsets = self.safeAreaInsets;
    }
    self.infoLabel.text = [NSString stringWithFormat:@"safeAreaInsets:%@",NSStringFromUIEdgeInsets(safeAreaInsets)];
    NSLog(@"TestOptionButtonsListViewCell.safeAreaInsets=%@",NSStringFromUIEdgeInsets(safeAreaInsets));
}
- (CGSize)sizeThatFits:(CGSize)size{
//    return CGSizeMake(size.width, 30);
    CGSize s = [self.flowlayout sizeThatFits:size resizeItems:YES];
    return s;
}
@end


@interface TestElementsView_GroupBackgrounView : UIView
@property(nonatomic,strong) UIVisualEffectView *backgroundEffectView;//半透明效果

@end
@implementation TestElementsView_GroupBackgrounView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.backgroundColor = UIColor.redColor;
        self.backgroundEffectView = [[UIVisualEffectView alloc] init];
        [self addSubview:self.backgroundEffectView];
        [self _MKUICustomTabBar_doTraitCollection];
    }
    return self;
}
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{
    [super traitCollectionDidChange:previousTraitCollection];
    [self _MKUICustomTabBar_doTraitCollection];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.backgroundEffectView.frame = self.bounds;
}
- (void)_MKUICustomTabBar_doTraitCollection{
    if (@available(iOS 12.0, *)) {
        if(self.traitCollection.userInterfaceStyle==UIUserInterfaceStyleDark){
            self.backgroundEffectView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        }else{
            self.backgroundEffectView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        }
    }else{
        self.backgroundEffectView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    }
}
@end

@interface TestElementsView_SeparatorView : UIView
@end
@implementation TestElementsView_SeparatorView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.backgroundColor = UIColor.systemYellowColor;
    }
    return self;
}
- (CGSize)sizeThatFits:(CGSize)size{
    return CGSizeMake(size.width, 2);
}
@end

@interface TestElementsViewController ()<MKUIListViewDelegate>
@property(nonatomic,strong) UITapGestureRecognizer *tapGesture;

@property(nonatomic,strong) UIColor *custom_groupBackgroundColor;
@property(nonatomic,assign) UIEdgeInsets custom_groupInsets;
@property(nonatomic,assign) UIEdgeInsets custom_groupInnerInsets;
@property(nonatomic,assign) CGFloat custom_groupCornerRadius;
@property(nonatomic,assign) CGFloat custom_cellInteritemSpacing;

@property(nonatomic,assign) MKUIListViewCellSeparatorStyle custom_separatorStyle;
@property(nonatomic,strong) UIColor *custom_separatorColor;
@property(nonatomic,strong) UIColor *custom_separatorBackgroundColor;
@property(nonatomic,assign) CGFloat custom_separatorSize;
@property(nonatomic,assign) UIEdgeInsets custom_separatorInsets;
@property(nonatomic,strong) Class custom_separatorViewClass;
@property(nonatomic,assign) NSInteger custom_section;
@property(nonatomic,readonly) BOOL isListViewDelegateSetted;
@property(nonatomic,assign) Class custom_groupBackgroudViewClass;
@end

@implementation TestElementsViewController
+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"ElementList表单" viewControllerClass:self];
}
//- (void)viewWillLayoutSubviews{
//    [super viewWillLayoutSubviews];
//    CGRect bounds = self.view.bounds;
//    UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 100, 0);
//    CGRect f1 = UIEdgeInsetsInsetRect(bounds, insets);
//    self.listView.frame = f1;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.custom_groupInsets = self.listView.groupInsets;
    self.custom_groupInnerInsets = self.listView.groupInnerInsets;
    self.custom_separatorSize = self.listView.separatorSize;
    self.custom_separatorColor = self.listView.separatorColor;
    self.custom_separatorStyle = self.listView.separatorStyle;
    self.custom_separatorInsets = self.listView.separatorInsets;
    self.custom_groupCornerRadius = self.listView.groupCornerRadius;
    self.custom_separatorViewClass = self.listView.separatorViewClass;
    self.custom_cellInteritemSpacing = self.listView.cellInteritemSpacing;
    self.custom_groupBackgroundColor = self.listView.groupBackgroundColor;
    self.custom_separatorBackgroundColor = self.listView.separatorBackgroundColor;
    
    self.listView.style = MKUIListViewStyleInsetGrouped;
    self.listView.delegate = nil;
    self.listView.backgroundView = [__TestListViewBackgroundView new];
    [self.listView deleteAllSections];
    @MK_WEAKIFY(self);
    {
        TestMKUIListViewSectionHeadFootView *head = [TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"listView的head"];
        self.listView.listHeaderView = head;
        TestMKUIListViewSectionHeadFootView *foot = [TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"listView的Foot"];
        self.listView.listFooterView = foot;
    }
    [self.listView addSection];
    [self.listView addSectionWithHeadView:[TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"分组A的head"] footView:[TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"分组A的Foot"]];
    {
        TestOptionButtonsListViewCell *optionsCell = [[TestOptionButtonsListViewCell alloc] init];
        @MK_WEAKIFY(optionsCell);
        TestButtonOption *option1 = [TestButtonOption optionWithText:@"菜单1" action:^(TestButtonOption * _Nonnull option) {
            @MK_NORMALIZE(optionsCell);
            option.text = [option.text isEqualToString:@"菜单1"]?@"菜单菜单1":@"菜单1";
            [optionsCell refreshButtons];
        }];
        TestButtonOption *option2 = [TestButtonOption optionWithText:@"菜单菜单2" action:^(TestButtonOption * _Nonnull option) {
            @MK_NORMALIZE(optionsCell);
            option.text = [option.text isEqualToString:@"菜单2"]?@"菜单菜单2":@"菜单2";
            [optionsCell refreshButtons];
        }];
        optionsCell.buttonOptions = @[
            option1,
            option2,
        ];
        [optionsCell refreshButtons];
        [self.listView addItem:optionsCell];
    }
    
    //添加空白的分组
    [self.listView addSection];
    [self.listView addSectionWithHeadView:[__TestEmptyListHeadFoot new] footView:[__TestEmptyListHeadFoot new]];
    for(int i=0;i<3;i++){
        [self.listView addItem:[__TestEmptyListCell new]];
    }
    //添加空白加元素
    [self.listView addSection];
    [self.listView addItem:[__TestSizeLabelCell new]];
    [self.listView addSectionWithHeadView:[TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"测试包含空白Cell的分组Head"] footView:[TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"测试包含空白Cell的分组Foot"]];
    for(int i=0;i<1;i++){
        [self.listView addItem:[__TestEmptyListCell new]];
    }
    {
        TestFunButtons *buttons = [[TestFunButtons alloc] init];
        [buttons addButton:@"上方添加空白Cell" click:^(TestFunButtons * _Nonnull buttons) {
            @MK_NORMALIZE(self);
            NSIndexPath *indexpath = [self.listView indexPathForCell:buttons];
            [self.listView insertItem:[__TestEmptyListCell new] atIndexPath:indexpath];
        }];
        [buttons addButton:@"下方添加空白Cell" click:^(TestFunButtons * _Nonnull buttons) {
            @MK_NORMALIZE(self);
            NSIndexPath *indexpath = [self.listView indexPathForCell:buttons];
            [self.listView insertItem:[__TestEmptyListCell new] atIndexPath:[NSIndexPath indexPathForItem:indexpath.item+1 inSection:indexpath.section]];
        }];
        [buttons addButton:@"上方添加Cell" click:^(TestFunButtons * _Nonnull buttons) {
            @MK_NORMALIZE(self);
            NSIndexPath *indexpath = [self.listView indexPathForCell:buttons];
            @MK_WEAKIFY(self);
            [self.listView insertItem:[MKUIElementListViewCellButton testButtonElementWithTitle:@"删除" buttonStyle:(MKUIElementViewButtonStyleDestructive) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
                @MK_NORMALIZE(self);
                [self.listView deleteView:el];
            }] atIndexPath:indexpath];
        }];
        [buttons addButton:@"下方添加Cell" click:^(TestFunButtons * _Nonnull buttons) {
            @MK_NORMALIZE(self);
            NSIndexPath *indexpath = [self.listView indexPathForCell:buttons];
            @MK_WEAKIFY(self);
            [self.listView insertItem:[MKUIElementListViewCellButton testButtonElementWithTitle:@"删除" buttonStyle:(MKUIElementViewButtonStyleDestructive) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
                @MK_NORMALIZE(self);
                [self.listView deleteView:el];
            }] atIndexPath:[NSIndexPath indexPathForItem:indexpath.item+1 inSection:indexpath.section]];
        }];
        [buttons addButton:@"删除上一个Cell" click:^(TestFunButtons * _Nonnull buttons) {
            @MK_NORMALIZE(self);
            NSIndexPath *indexpath = [self.listView indexPathForCell:buttons];
            [self.listView deleteItemAtIndexPath:[NSIndexPath indexPathForRow:indexpath.row-1 inSection:indexpath.section] withAnimation:(MKUIListViewItemAnimationAutomatic)];
        }];
        [buttons addButton:@"删除下一个Cell" click:^(TestFunButtons * _Nonnull buttons) {
            @MK_NORMALIZE(self);
            NSIndexPath *indexpath = [self.listView indexPathForCell:buttons];
            [self.listView deleteItemAtIndexPath:[NSIndexPath indexPathForRow:indexpath.row+1 inSection:indexpath.section] withAnimation:(MKUIListViewItemAnimationAutomatic)];
        }];
        [buttons addButton:@"删除该分组Cells" click:^(TestFunButtons * _Nonnull buttons) {
            @MK_NORMALIZE(self);
            NSIndexPath *indexpath = [self.listView indexPathForCell:buttons];
            NSArray *cells = [self.listView cellsForItemsInSectionIndex:indexpath.section];
            for(UIView *c in cells){
                [self.listView deleteItem:c withAnimation:(MKUIListViewItemAnimationAutomatic)];
            }
        }];
        [self.listView addItem:buttons];
    }
    for(int i=0;i<1;i++){
        [self.listView addItem:[__TestEmptyListCell new]];
    }
    
    //
    [self.listView addSection];
    [self.listView addSectionWithHeadView:[TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"分组B的head"] footView:nil];
    [self.listView addSection];
    [self.listView addSectionWithHeadView:nil footView:[TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"分组C的Foot"]];
    
    [self.listView addSection];
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"是否使用PlainStyle" on:self.listView.style==MKUIListViewStylePlain whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.listView.style = on?MKUIListViewStylePlain:MKUIListViewStyleInsetGrouped;
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"设置plainStyleUseGroupInsets" on:self.listView.plainStyleUseGroupInsets whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.listView.plainStyleUseGroupInsets = on;
        }];
        [self.listView addItem:el];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"设置sectionInteritemSpacing" value:self.listView.sectionInteritemSpacing minimumValue:0 maximumValue:20 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
            @MK_NORMALIZE(self);
            self.listView.sectionInteritemSpacing = value;
        }]];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider_UIEdgeInsets testSliderElementWithTitle:@"设置contentInset" value:self.listView.contentInset minimumValue:UIEdgeInsetsMake(-100, -100, -100, -100) maximumValue:UIEdgeInsetsMake(100, 100, 100, 100) whenChange:^(TestMKUIElementViewSlider_UIEdgeInsets * _Nonnull el, UIEdgeInsets value) {
            @MK_NORMALIZE(self);
            self.listView.contentInset = value;
            [self.listView reloadData];
        }]];
    }
    {
        [self.listView addItem:[MKUIElementListViewCellSelection testUIColorElementWithTitle:@"设置backgroundViewColor" selected:self.listView.backgroundView.backgroundColor whenDone:^(UIColor * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.listView.backgroundView.backgroundColor = selectedValue;
        }]];
    }
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"设置Delegate" on:self.isListViewDelegateSetted whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.listView.delegate = on?self:nil;
            [self.listView reloadData];
        }];
        [self.listView addItem:el];
    }
    [self _testElementCustomSection];
    [self _testElementSetSectionPropertis];
    [self _testElementText];
    [self _testElementImage];
    [self _testSwitchElements];
    [self _testElementSelect];
    [self _testElementTextField];
    [self _testElementTextArea];
    [self _testElementTextInput];
    [self _testElementButton];
    
    [self.listView addSection];
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"刷新" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            @MK_NORMALIZE(self);
            [self.listView reloadData];
        }];
        [self.listView addItem:el];
    }
    [self.listView addSection];
    [self.listView addSection];
    [self.listView reloadData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(__onKeyboardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(__onKeyboardChange:) name:UIKeyboardDidChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(__onKeyboardHide:) name:UIKeyboardWillHideNotification object:nil];
    //
    self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(__onTap:)];
    self.tapGesture.enabled = NO;
    [self.view addGestureRecognizer:self.tapGesture];
}
- (void)__onTap:(id)sender{
    [self.view endEditing:YES];
}
- (void)__onKeyboardShow:(NSNotification *)noti{
    self.tapGesture.enabled = YES;
    [self.listView mk_adjustContentInsetWithUIKeyboardNotification:noti contentInset:UIEdgeInsetsZero window:nil];
    [self.listView mk_adjustContentOffsetWithUIKeyboardNotification:noti responderViewClass:MKUIListViewAttributeContainerView.class padding:40 window:nil];
}
- (void)__onKeyboardChange:(NSNotification *)noti{
    [self.listView mk_adjustContentInsetWithUIKeyboardNotification:noti contentInset:UIEdgeInsetsZero window:nil];
    [self.listView mk_adjustContentOffsetWithUIKeyboardNotification:noti responderViewClass:MKUIListViewAttributeContainerView.class padding:40 window:nil];
}
- (void)__onKeyboardHide:(NSNotification *)noti{
    self.tapGesture.enabled = NO;
    UIEdgeInsets insets = self.listView.contentInset;
    insets.bottom = 0;
    self.listView.contentInset = insets;
}
- (BOOL)isListViewDelegateSetted{
    return self.listView.delegate!=nil;
}
- (void)_testElementSetSectionPropertis{
    [self.listView addSection];
    NSInteger section = self.listView.numberOfSections;
    @MK_WEAKIFY(self);
    {
        TestMKUIListViewSectionHeadFootView *head = [TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"单独设置该分组的属性Begin"];
        [self.listView setSectionView:head atIndex:section kind:(MKUIListViewSectionViewKindOfHead)];
        TestMKUIListViewSectionHeadFootView *foot = [TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"单独设置该分组的属性End"];
        [self.listView setSectionView:foot atIndex:section kind:(MKUIListViewSectionViewKindOfFoot)];
    }
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"是否使用自定义groupBackgroundClass" on:[self.listView groupBackgroundColorForSection:section]!=nil whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            Class custom_groupBackgroudViewClass = on?TestElementsView_GroupBackgrounView.class:nil;
            [self.listView setGroupBackgroundViewClass:custom_groupBackgroudViewClass forSection:section];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"设置groupBackgroundColor" selected:[self.listView groupBackgroundColorForSection:section] whenDone:^(UIColor * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            [self.listView setGroupBackgroundColor:selectedValue forSection:section];
        }];
        [self.listView addItem:el];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider_UIEdgeInsets testSliderElementWithTitle:@"设置groupInsets" value:[self.listView groupInsetsForSection:section] minimumValue:UIEdgeInsetsZero maximumValue:MKUIEdgeInsetsMakeSameEdge(20) whenChange:^(TestMKUIElementViewSlider_UIEdgeInsets * _Nonnull el, UIEdgeInsets value) {
            @MK_NORMALIZE(self);
            [self.listView setGroupInsets:value forSection:section];
        }]];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider_UIEdgeInsets testSliderElementWithTitle:@"设置groupInnerInsets" value:[self.listView groupInnerInsetsForSection:section] minimumValue:UIEdgeInsetsZero maximumValue:MKUIEdgeInsetsMakeSameEdge(20) whenChange:^(TestMKUIElementViewSlider_UIEdgeInsets * _Nonnull el, UIEdgeInsets value) {
            @MK_NORMALIZE(self);
            [self.listView setGroupInnerInsets:value forSection:section];
        }]];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"设置groupCornerRadius" value:[self.listView groupCornerRadiusForSection:section] minimumValue:0 maximumValue:40 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
            @MK_NORMALIZE(self);
            [self.listView setGroupCornerRadius:value forSection:section];
        }]];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"设置cellInteritemSpacing" value:[self.listView cellInteritemSpacingForSection:section] minimumValue:0 maximumValue:40 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
            @MK_NORMALIZE(self);
            [self.listView setCellInteritemSpacing:value forSection:section];
        }]];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testElementMKUIListViewCellSeparatorStyle:@"设置separatorStyle" selected:[self.listView separatorStyleForSection:section] whenDone:^(MKUIListViewCellSeparatorStyle selectedValue) {
            @MK_NORMALIZE(self);
            [self.listView setSeparatorStyle:selectedValue forSection:section];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"设置separatorColor" selected:[self.listView separatorColorForSection:section] whenDone:^(UIColor * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            [self.listView setSeparatorColor:selectedValue forSection:section];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"设置separatorBackgroundColor" selected:[self.listView separatorBackgroundColorForSection:section] whenDone:^(UIColor * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            [self.listView setSeparatorBackgroundColor:selectedValue forSection:section];
        }];
        [self.listView addItem:el];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"设置separatorSize" value:[self.listView separatorSizeForSection:section] minimumValue:0 maximumValue:40 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
            @MK_NORMALIZE(self);
            [self.listView setSeparatorSize:value forSection:section];
        }]];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider_UIEdgeInsets testSliderElementWithTitle:@"设置separatorInsets" value:[self.listView separatorInsetsForSection:section] minimumValue:UIEdgeInsetsZero maximumValue:MKUIEdgeInsetsMakeSameEdge(20) whenChange:^(TestMKUIElementViewSlider_UIEdgeInsets * _Nonnull el, UIEdgeInsets value) {
            @MK_NORMALIZE(self);
            [self.listView setSeparatorInsets:value forSection:section];
        }]];
    }
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"设置separatorViewClass" on:[self.listView separatorViewClassForSection:section]!=nil whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            [self.listView setSeparatorViewClass:on?TestElementsView_SeparatorView.class:nil forSection:section];
        }];
        [self.listView addItem:el];
    }
}
- (void)_testElementCustomSection{
    [self.listView addSection];
    self.custom_section = self.listView.numberOfSections;
    @MK_WEAKIFY(self);
    {
        TestMKUIListViewSectionHeadFootView *head = [TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"设置Delegate的属性Begin"];
        [self.listView setSectionView:head atIndex:self.custom_section kind:(MKUIListViewSectionViewKindOfHead)];
        TestMKUIListViewSectionHeadFootView *foot = [TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"设置Delegate的属性End"];
        [self.listView setSectionView:foot atIndex:self.custom_section kind:(MKUIListViewSectionViewKindOfFoot)];
    }
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"是否使用自定义groupBackgroundClass" on:self.custom_groupBackgroudViewClass!=nil whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.custom_groupBackgroudViewClass = on?TestElementsView_GroupBackgrounView.class:nil;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"设置groupBackgroundColor" selected:self.custom_groupBackgroundColor whenDone:^(UIColor * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.custom_groupBackgroundColor = selectedValue;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider_UIEdgeInsets testSliderElementWithTitle:@"设置groupInsets" value:self.custom_groupInsets minimumValue:(UIEdgeInsetsZero) maximumValue:MKUIEdgeInsetsMakeSameEdge(20) whenChange:^(TestMKUIElementViewSlider_UIEdgeInsets * _Nonnull el, UIEdgeInsets value) {
            @MK_NORMALIZE(self);
            self.custom_groupInsets = value;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }]];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider_UIEdgeInsets testSliderElementWithTitle:@"设置groupInnerInsets" value:self.custom_groupInnerInsets minimumValue:(UIEdgeInsetsZero) maximumValue:MKUIEdgeInsetsMakeSameEdge(20) whenChange:^(TestMKUIElementViewSlider_UIEdgeInsets * _Nonnull el, UIEdgeInsets value) {
            @MK_NORMALIZE(self);
            self.custom_groupInnerInsets = value;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }]];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"设置groupCornerRadius" value:self.custom_groupCornerRadius minimumValue:0 maximumValue:40 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
            @MK_NORMALIZE(self);
            self.custom_groupCornerRadius = value;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }]];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"设置cellInteritemSpacing" value:self.custom_cellInteritemSpacing minimumValue:0 maximumValue:20 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
            @MK_NORMALIZE(self);
            self.custom_cellInteritemSpacing = value;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }]];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testElementMKUIListViewCellSeparatorStyle:@"设置separatorStyle" selected:self.custom_separatorStyle whenDone:^(MKUIListViewCellSeparatorStyle selectedValue) {
            @MK_NORMALIZE(self);
            self.custom_separatorStyle = selectedValue;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"设置separatorColor" selected:self.custom_separatorColor whenDone:^(UIColor * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.custom_separatorColor = selectedValue;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"设置separatorBackgroundColor" selected:self.custom_separatorBackgroundColor whenDone:^(UIColor * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.custom_separatorBackgroundColor = selectedValue;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"设置separatorSize" value:self.custom_separatorSize minimumValue:0 maximumValue:20 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
            @MK_NORMALIZE(self);
            self.custom_separatorSize = value;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }]];
    }
    {
        [self.listView addItem:[TestMKUIElementViewSlider_UIEdgeInsets testSliderElementWithTitle:@"设置separatorInsets" value:self.custom_separatorInsets minimumValue:(UIEdgeInsetsZero) maximumValue:MKUIEdgeInsetsMakeSameEdge(20) whenChange:^(TestMKUIElementViewSlider_UIEdgeInsets * _Nonnull el, UIEdgeInsets value) {
            @MK_NORMALIZE(self);
            self.custom_separatorInsets = value;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }]];
    }
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"设置separatorViewClass" on:self.custom_separatorViewClass!=nil whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.custom_separatorViewClass = on?TestElementsView_SeparatorView.class:nil;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    
}
- (void)_testElementText{
    [self.listView addSection];
    [self.listView addSection];
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"标题1";
        el.elementView.subTitleText = @"子标题1";
        el.elementView.detailText = @"详细内容1";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.whenClick = ^(MKUIElementListViewCellDetail * _Nonnull el) {
            el.elementView.accessView = el.elementView.accessView?nil:el.elementView.disclosureIndicatorAccessView;
            el.elementView.subTitleText = el.elementView.subTitleText.length>0?nil:@"子标题1";
            if(UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, el.elementView.iconImageMarggin)){
                el.elementView.iconImageSize = CGSizeMake(60, 30);
                el.elementView.iconImageMarggin = UIEdgeInsetsMake(10, 10, 10, 10);
            }else{
                el.elementView.iconImageSize = CGSizeMake(28, 28);
                el.elementView.iconImageMarggin = UIEdgeInsetsZero;
            }
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
    [self.listView addSection];
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"标题1";
        el.elementView.subTitleText = @"子标题1";
        el.elementView.detailText = @"详细内容1";
        el.elementView.accessView = el.elementView.checkmarkAccessView;
        el.whenClick = ^(MKUIElementListViewCellDetail * _Nonnull el) {
            el.elementView.detailText = [NSString stringWithFormat:@"%@详细内容1",el.elementView.detailText];
            el.elementView.accessView = el.elementView.accessView?nil:el.elementView.checkmarkAccessView;
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"标题1";
        el.elementView.subTitleText = @"子标题1";
        el.elementView.detailText = @"详细内容1";
        el.elementView.accessView = el.elementView.checkmarkAccessView;
        el.whenClick = ^(MKUIElementListViewCellDetail * _Nonnull el) {
            el.elementView.detailText = [NSString stringWithFormat:@"%@详细内容1",el.elementView.detailText];
            el.elementView.accessView = el.elementView.accessView?nil:el.elementView.checkmarkAccessView;
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
}
- (void)_testSwitchElements{
    [self.listView addSection];
    if (@available(iOS 13.0, *)){
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.titleText = @"是否黑暗模式";
        el.elementView.on = self.traitCollection.userInterfaceStyle==UIUserInterfaceStyleDark;
        @MK_WEAKIFY(self);
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
            @MK_NORMALIZE(self);
            self.overrideUserInterfaceStyle = on?UIUserInterfaceStyleDark:UIUserInterfaceStyleLight;
        };
        [self.listView addItem:el];
    }
    if (@available(iOS 13.0, *)){
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.titleText = @"group背景透明";
        el.elementView.on = NO;
        @MK_WEAKIFY(self);
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
            @MK_NORMALIZE(self);
            self.listView.groupBackgroundColor = on?nil:UIColor.mk_listViewGroupBackgroundColor;
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"listView分隔线颜色";
        @MK_WEAKIFY(self);
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
            @MK_NORMALIZE(self);
            self.listView.separatorColor = on?[UIColor mk_colorWithLight:UIColor.systemRedColor dark:UIColor.systemBlueColor]:UIColor.mk_listViewSeparatorColor;
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.iconImage = nil;
        el.elementView.titleText = @"Switch标题";
        el.elementView.on = YES;
        @MK_WEAKIFY(el);
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el2, BOOL on) {
            @MK_NORMALIZE(el);
            el.elementView.hiddenEmptyIconImage = el.elementView.isOn;
            el.elementView.subTitleText = el.elementView.isOn?@"Switch选中":@"Switch未选中";
            [el mk_reloadInListView];
        };

        //CGColor不能使用动态颜色，不会随黑暗模式变更而变色。
        MKUIFlowLayoutButton *button = [[MKUIFlowLayoutButton alloc] init];
        button.layer.borderWidth = 1;
        button.layer.borderColor = [UIColor mk_colorWithLight:UIColor.redColor dark:UIColor.blueColor].CGColor;
        [button setTitleColor:[UIColor mk_colorWithLight:UIColor.redColor dark:UIColor.blueColor] forState:(UIControlStateNormal)];
        [button setTitle:@"按钮" forState:(UIControlStateNormal)];
        button.frame = CGRectMake(0, 0, 60, 40);
        el.elementView.accessView = button;
        [self.listView addItem:el];
    }
}
- (void)_testElementButton{
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [[MKUIElementListViewCellButton alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"Button标题1";
        el.whenClick = ^(MKUIElementListViewCellButton * _Nonnull el) {
            el.elementView.titleText = [NSString stringWithFormat:@"%@%@",el.elementView.titleText,el.elementView.titleText];
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }{
        MKUIElementListViewCellButton *el = [[MKUIElementListViewCellButton alloc] init];
        el.elementView.titleText = @"Button标题1";
        el.whenClick = ^(MKUIElementListViewCellButton * _Nonnull el) {
            el.elementView.buttonStyle = (el.elementView.buttonStyle+1)%3;
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
}
- (void)_testElementSelect{
    [self.listView addSection];

    NSArray<MKUIElementSelectOption *> *options = [MKUIElementSelectOption optionsWithTexts:@[
        @"选项1",
        @"选项2",
        @"选项3",
        @"选项4",
    ]];
    {
        MKUIElementListViewCellSelection *el = [[MKUIElementListViewCellSelection alloc] init];
        el.elementView.titleText = @"单选2";
        el.elementView.allowsMultipleSelection = NO;
        el.elementView.options = options;
        el.elementView.doneButtonText = @"完成";
        el.elementView.selectedValue = options.lastObject.value;
        @MK_WEAKIFY(el);
        el.elementView.whenDone = ^(__kindof MKUIElementViewSelection * _Nonnull el2) {
            @MK_NORMALIZE(el);
            NSLog(@"selected:%@",el.elementView.selectedValue);
            [el mk_reloadInListView];
        };  
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [[MKUIElementListViewCellSelection alloc] init];
        el.elementView.titleText = @"多选2";
        el.elementView.allowsMultipleSelection = YES;
        el.elementView.options = options;
        el.elementView.detailTextWhenNoSelected = @"未选择";
        @MK_WEAKIFY(el);
        el.elementView.whenDone = ^(__kindof MKUIElementViewSelection * _Nonnull el2) {
            @MK_NORMALIZE(el);
            NSLog(@"selected:%@",[el.elementView.selectedValues componentsJoinedByString:@","]);
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
}
- (void)_testElementTextField{
    {
        [self.listView addSection];
        MKUIElementListViewCellTextField *el = [[MKUIElementListViewCellTextField alloc] init];
        el.elementView.textField.textAlignment = NSTextAlignmentLeft;
        el.elementView.textField.placeholder = @"请输入";
        @MK_WEAKIFY(el);
        el.elementView.whenChange = ^(__kindof MKUIElementViewTextField * _Nonnull el2, NSString * _Nonnull text) {
            @MK_NORMALIZE(el);
            NSLog(@"text:%@",text);
            [el mk_reloadInListView];
        };
        el.elementView.whenReturn = ^(MKUIElementViewTextField * _Nonnull el2) {
            @MK_NORMALIZE(el);
            NSLog(@"return text:%@",el.elementView.text);
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
    {
        [self.listView addSection];
        MKUIElementListViewCellTextField *el = [[MKUIElementListViewCellTextField alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"输入单行文本";
        el.elementView.textField.placeholder = @"请输入";
        @MK_WEAKIFY(el);
        el.elementView.whenChange = ^(__kindof MKUIElementViewTextField * _Nonnull el2, NSString * _Nonnull text) {
            @MK_NORMALIZE(el);
            NSLog(@"text:%@",text);
            if(text.length>20){
                text = [text substringToIndex:20];
                el.elementView.text = text;
            }
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
}
- (void)_testElementTextArea{
    {
        [self.listView addSection];
        MKUIElementListViewCellTextArea *el = [[MKUIElementListViewCellTextArea alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"输入多行文本";
        @MK_WEAKIFY(el);
        el.elementView.whenChange = ^(__kindof MKUIElementViewTextArea * _Nonnull el2, NSString * _Nonnull text) {
            @MK_NORMALIZE(el);
            NSLog(@"text:%@",text);
            if(text.length>20){
                text = [text substringToIndex:20];
                el2.text = text;
            }
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
    {
        [self.listView addSection];
        MKUIElementListViewCellTextArea *el = [[MKUIElementListViewCellTextArea alloc] init];
        el.elementView.iconImage = nil;
        el.elementView.titleText = nil;
        el.elementView.textView.textAlignment = NSTextAlignmentLeft;
        el.elementView.doneButtonText = @"完成";
        @MK_WEAKIFY(el);
        el.elementView.whenDone = ^(__kindof MKUIElementViewTextArea * _Nonnull el2) {
            @MK_NORMALIZE(el);
            NSLog(@"text view done:%@",el2.text);
            [el mk_reloadInListView];
        };
        el.elementView.whenChange = ^(__kindof MKUIElementViewTextArea * _Nonnull el2, NSString * _Nonnull text) {
            @MK_NORMALIZE(el);
            NSLog(@"text:%@",text);
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
}
- (void)_testElementTextInput{
    [self.listView addSection];
    {
        MKUIElementListViewCellTextInput *el = [[MKUIElementListViewCellTextInput alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"输入单行文本";
        el.elementView.placeholder = @"请输入";
        el.elementView.allowsMultipleLineText = NO;
//        @MK_WEAKIFY(self);
        @MK_WEAKIFY(el);
        el.elementView.whenChange = ^NSString * _Nonnull(__kindof MKUIElementViewTextInput * _Nonnull el2, NSString * _Nonnull text) {
//            @MK_NORMALIZE(self);
            if(text.length>20){
                return [text substringToIndex:20];
            }
            return text;
        };
        el.elementView.whenDone = ^(__kindof MKUIElementViewTextInput * _Nonnull el2) {
            @MK_NORMALIZE(el);
            NSLog(@"done:%@",el2.text);
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellTextInput *el = [[MKUIElementListViewCellTextInput alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"输入多行文本";
        el.elementView.allowsMultipleLineText = YES;
        el.elementView.doneButtonText = @"完成";
//        @MK_WEAKIFY(self);
        el.elementView.whenChange = ^NSString * _Nonnull(__kindof MKUIElementViewTextInput * _Nonnull el, NSString * _Nonnull text) {
//            @MK_NORMALIZE(self);
            NSLog(@"change:%@",text);
            return text;
        };
        @MK_WEAKIFY(el);
        el.elementView.whenDone = ^(__kindof MKUIElementViewTextInput * _Nonnull el2) {
            @MK_NORMALIZE(el);
            NSLog(@"done:%@",el2.text);
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
}
- (void)_testElementImage{
    [self.listView addSection];
    {
        MKUIElementListViewCellImage *el = [[MKUIElementListViewCellImage alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"图片";
        el.elementView.subTitleText = @"子标题1";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.elementView.detailImage = [Menu randomImage];
        el.whenClick = ^(MKUIElementListViewCellImage *el) {
            el.elementView.subTitleText = [NSString stringWithFormat:@"%@子标题1",el.elementView.subTitleText];
            el.elementView.detailImage = el.elementView.detailImage?nil:[Menu randomImage];
            el.elementView.detailImageSize = CGSizeMake(40, 40);
            el.elementView.detailImageMarggin = UIEdgeInsetsMake(0, 5, 0, 5);
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellImage *el = [[MKUIElementListViewCellImage alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"图片";
        el.elementView.subTitleText = @"子标题1";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.elementView.detailImage = [Menu randomImage];
        el.whenClick = ^(MKUIElementListViewCellImage *el) {
            el.elementView.subTitleText = [NSString stringWithFormat:@"%@子标题1",el.elementView.subTitleText];
            el.elementView.detailImage = el.elementView.detailImage?nil:[Menu randomImage];
            el.elementView.detailImageSize = CGSizeMake(40, 40);
            el.elementView.detailImageMarggin = UIEdgeInsetsMake(0, 5, 0, 5);
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellImage *el = [[MKUIElementListViewCellImage alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"图片2";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.elementView.detailImage = [Menu randomImage];
        el.whenClick = ^(MKUIElementListViewCellImage *el) {
            if(UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, el.elementView.detailImageMarggin)){
                el.elementView.detailImageSize = CGSizeMake(40, 40);
                el.elementView.detailImageMarggin = UIEdgeInsetsMake(0, 5, 0, 5);
            }else{
                el.elementView.detailImageSize = CGSizeMake(28, 28);
                el.elementView.detailImageMarggin = UIEdgeInsetsZero;
            }
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
}
#pragma mark - delegate:MKUIListViewDelegate
#pragma mark - 定制各分组的属性
- (nullable Class)listView:(MKUIListView *)listView groupBackgroundViewClassForSetion:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_groupBackgroudViewClass:nil;
}
- (nullable UIColor *)listView:(MKUIListView *)listView groupBackgroundColorForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_groupBackgroundColor:listView.groupBackgroundColor;
}
- (UIEdgeInsets)listView:(MKUIListView *)listView groupInsetsForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_groupInsets:listView.groupInsets;
}
- (UIEdgeInsets)listView:(MKUIListView *)listView groupInnerInsetsForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_groupInnerInsets:listView.groupInnerInsets;
}
- (CGFloat)listView:(MKUIListView *)listView groupCornerRadiusForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_groupCornerRadius:listView.groupCornerRadius;
}
- (CGFloat)listView:(MKUIListView *)listView cellInteritemSpacingForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_cellInteritemSpacing:listView.cellInteritemSpacing;
}

#pragma mark - 定制各分组的分隔线属性
- (BOOL)isCustomSection:(NSInteger)section{
    return YES;
//    if(section>=self.custom_section && section<=self.custom_section+5) return YES;
//    return NO;
}
- (MKUIListViewCellSeparatorStyle)listView:(MKUIListView *)listView separatorStyleForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_separatorStyle:listView.separatorStyle;
}
- (nullable UIColor *)listView:(MKUIListView *)listView separatorColorForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_separatorColor:listView.separatorColor;
}
- (nullable UIColor *)listView:(MKUIListView *)listView separatorBackgroundColorForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_separatorBackgroundColor:listView.separatorBackgroundColor;
}
- (CGFloat)listView:(MKUIListView *)listView separatorSizeForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_separatorSize:listView.separatorSize;
}
- (UIEdgeInsets)listView:(MKUIListView *)listView separatorInsetsForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_separatorInsets:listView.separatorInsets;
}
- (nullable Class)listView:(MKUIListView *)listView separatorViewClassForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_separatorViewClass:listView.separatorViewClass;
}
@end
